/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.alibaba.fastjson.JSON;
import me.zhengjie.modules.biz.domain.*;
import me.zhengjie.modules.biz.repository.*;
import me.zhengjie.modules.biz.service.mapstruct.FreshAirVentilatorLogMapper;
import me.zhengjie.modules.biz.utils.NettyServerHandler;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.service.FreshAirVentilatorService;
import me.zhengjie.modules.biz.service.dto.FreshAirVentilatorDto;
import me.zhengjie.modules.biz.service.dto.FreshAirVentilatorQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.FreshAirVentilatorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.lang.System;
import java.util.*;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class FreshAirVentilatorServiceImpl implements FreshAirVentilatorService {

    private final FreshAirVentilatorRepository freshAirVentilatorRepository;
    private final FreshAirVentilatorMapper freshAirVentilatorMapper;
    private final LabRepository labRepository;
    private final FreshAirVentilatorLogRepository freshAirVentilatorLogRepository;
    private final FreshAirVentilatorLogMapper freshAirVentilatorLogMapper;
    private final ErrorLogRepository errorLogRepository;
    private final ExhaustFanRepository exhaustFanRepository;


    @Override
    public Map<String,Object> queryAll(FreshAirVentilatorQueryCriteria criteria, Pageable pageable){
        Page<FreshAirVentilator> page = freshAirVentilatorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<FreshAirVentilatorDto> dto = page.map(freshAirVentilatorMapper::toDto);
        List<FreshAirVentilatorDto> resultDto = dto.getContent();
//        for (FreshAirVentilatorDto freshAirVentilatorDto:resultDto) {
//            Lab lab = labRepository.findById(freshAirVentilatorDto.getLabId()).orElseGet(Lab::new);
//            freshAirVentilatorDto.setLabName(lab.getName());
//        }
        return PageUtil.toPage(dto);
    }

    @Override
    public List<FreshAirVentilatorDto> queryAll(FreshAirVentilatorQueryCriteria criteria){
        List<FreshAirVentilator> freshAirVentilatorList = freshAirVentilatorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        List<FreshAirVentilatorDto> freshAirVentilatorDtoList = freshAirVentilatorMapper.toDto(freshAirVentilatorList);
        return freshAirVentilatorDtoList;
    }

    @Override
    @Transactional
    public FreshAirVentilatorDto findById(Integer id) {
        FreshAirVentilator freshAirVentilator = freshAirVentilatorRepository.findById(id).orElseGet(FreshAirVentilator::new);
        ValidationUtil.isNull(freshAirVentilator.getId(),"FreshAirVentilator","id",id);
        return freshAirVentilatorMapper.toDto(freshAirVentilator);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FreshAirVentilatorDto create(FreshAirVentilator resources) {
        return freshAirVentilatorMapper.toDto(freshAirVentilatorRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FreshAirVentilator resources) {
        FreshAirVentilator freshAirVentilator = freshAirVentilatorRepository.findById(resources.getId()).orElseGet(FreshAirVentilator::new);
        ValidationUtil.isNull( freshAirVentilator.getId(),"FreshAirVentilator","id",resources.getId());
        freshAirVentilator.copy(resources);
        freshAirVentilatorRepository.save(freshAirVentilator);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            freshAirVentilatorRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FreshAirVentilatorDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FreshAirVentilatorDto freshAirVentilator : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", freshAirVentilator.getName());
            map.put("新风设备ip", freshAirVentilator.getIp());
            map.put("楼层", freshAirVentilator.getFloor());
            if (freshAirVentilator.getIndoorTemperature() != 0) {
                map.put("室内温度", freshAirVentilator.getIndoorTemperature() + "℃");
            } else {
                map.put("室内温度", "-");
            }
            if (freshAirVentilator.getAmbientTemperature() != 0) {
                map.put("环境温度", freshAirVentilator.getAmbientTemperature() + "℃");

            } else {
                map.put("环境温度", "-");
            }
            if (freshAirVentilator.getIsOpen() != null) {
                String isOpen = freshAirVentilator.getIsOpen() == 0 ? "停止" : "启动";
                map.put("启停状态", isOpen);
            }
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<FreshAirVentilatorDto> queryFreshAirVentilatorFloor(Integer floor) {
        List<FreshAirVentilatorDto> list = freshAirVentilatorMapper.toDto(freshAirVentilatorRepository.findByFloor(floor));
//        for (FreshAirVentilatorDto freshAirVentilatorDto:list) {
//            Lab lab = labRepository.findById(freshAirVentilatorDto.getLabId()).orElseGet(Lab::new);
//            freshAirVentilatorDto.setLabName(lab.getName());
//        }

        return list;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void openFreshAirVentilator(int id) {
        freshAirVentilatorRepository.updateById(id, 1);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void closeFreshAirVentilator(int id) {
        freshAirVentilatorRepository.updateById(id, 0);
    }

    @Override
    public int control(int id) {
        return freshAirVentilatorRepository.findIsOpenById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String,Integer> freshAirVentilatorTemp(int id, int indoorTemperature, int ambientTemperature,String errorOne, String errorTwo, String errorThree) {

        freshAirVentilatorRepository.updateTemperatureById(id,(float)indoorTemperature/10,(float) ambientTemperature/10);
        Map<String,Integer> open = new HashMap<>();
            long time = System.currentTimeMillis()/1000;
            if (time%180<5){
                FreshAirVentilator freshAirVentilator = freshAirVentilatorRepository.findById(id).orElseGet(FreshAirVentilator::new);
                FreshAirVentilatorLog freshAirVentilatorLog = new FreshAirVentilatorLog();
                freshAirVentilatorLog.setFreshAirVentilatorId(id);
                freshAirVentilatorLog.setAmbientTemperature((float) ambientTemperature/10);
                freshAirVentilatorLog.setIndoorTemperature((float)indoorTemperature/10);
                freshAirVentilatorLog.setIsOpen(freshAirVentilator.getIsOpen());
                freshAirVentilatorLog.setFloor(freshAirVentilator.getFloor());
                freshAirVentilatorLog.setName(freshAirVentilator.getName());
                freshAirVentilatorLogRepository.save(freshAirVentilatorLog);
//                if (errorOne.charAt(0)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("回风温度探头故障");
//                    errorLog.setDec("回风温度探头故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorOne.charAt(1)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("预处理后温度探头故障");
//                    errorLog.setDec("预处理后温度探头故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorOne.charAt(2)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("送风温度探头故障");
//                    errorLog.setDec("送风温度探头故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorOne.charAt(4)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("预处理盘管出管温度探头故障");
//                    errorLog.setDec("预处理盘管出管温度探头故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorOne.charAt(5)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("预处理盘管液管温度探头故障");
//                    errorLog.setDec("预处理盘管液管温度探头故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorOne.charAt(6)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("环境温度探头故障");
//                    errorLog.setDec("环境温度探头故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorOne.charAt(7)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("蒸发盘管一出管温度探头故障");
//                    errorLog.setDec("蒸发盘管一出管温度探头故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorOne.charAt(14)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("蒸发盘管二出管温度探头故障");
//                    errorLog.setDec("蒸发盘管二出管温度探头故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorOne.charAt(15)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("湿度探头故障");
//                    errorLog.setDec("湿度探头故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }
//
//                if(errorTwo.charAt(2)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("触摸屏通信故障");
//                    errorLog.setDec("触摸屏通信故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorTwo.charAt(3)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("外机通信故障");
//                    errorLog.setDec("外机通信故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorTwo.charAt(4)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("风压传感器故障");
//                    errorLog.setDec("风压传感器故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorTwo.charAt(5)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("风速传感器故障");
//                    errorLog.setDec("风速传感器故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorTwo.charAt(6)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("房间温度过低");
//                    errorLog.setDec("房间温度过低");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorTwo.charAt(7)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("房间温度过高");
//                    errorLog.setDec("房间温度过高");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorTwo.charAt(9)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("膨胀阈板一通信故障");
//                    errorLog.setDec("膨胀阈板一通信故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorTwo.charAt(10)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("膨胀阈板二通信故障");
//                    errorLog.setDec("膨胀阈板二通信故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }
//
//                if (errorThree.charAt(0)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("电源故障");
//                    errorLog.setDec("电源故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(1)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("送风机故障");
//                    errorLog.setDec("送风机故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(2)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("排风机故障");
//                    errorLog.setDec("排风机故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(3)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("预处理电加热过载");
//                    errorLog.setDec("预处理电加热过载");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(4)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("电加热过载");
//                    errorLog.setDec("电加热过载");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(6)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("一级滤网脏堵");
//                    errorLog.setDec("一级滤网脏堵");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(7)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("二级滤网脏堵");
//                    errorLog.setDec("二级滤网脏堵");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(8)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("失风报警");
//                    errorLog.setDec("失风报警");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(10)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("火灾报警");
//                    errorLog.setDec("火灾报警");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(12)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("送风机变频器故障");
//                    errorLog.setDec("送风机变频器故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(13)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("排风机变频器故障");
//                    errorLog.setDec("排风机变频器故障");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }else if (errorThree.charAt(14)=='1'){
//                    ErrorLog errorLog = new ErrorLog();
//                    errorLog.setName(freshAirVentilator.getName());
//                    errorLog.setPosition(freshAirVentilator.getAddress());
//                    errorLog.setErrorCode("水浸报警");
//                    errorLog.setDec("水浸报警");
//                    errorLog.setType(1);
//                    errorLog.setValue(1);
//                    errorLog.setFloor(freshAirVentilator.getFloor());
//                    errorLog.setCreatedAt(DateUtil.strToSqlDate(String.valueOf(System.currentTimeMillis())));
//                    errorLogRepository.save(errorLog);
//                }
            }


        open.put("is_open",freshAirVentilatorRepository.findIsOpenById(id));
        return open;
    }

    @Override
    public Integer sendMsgToClient(Integer id, Integer isOpen) {
        FreshAirVentilator freshAirVentilator = freshAirVentilatorRepository.findById(id).orElseGet(FreshAirVentilator::new);
        String ipAndPort = "/"+freshAirVentilator.getIp().concat(":4198");
        Map<String,Integer> content = new HashMap<>();
        content.put("is_open",isOpen);
        AtomicInteger flag = new AtomicInteger();
        freshAirVentilator.setIsOpen(isOpen);
        freshAirVentilatorRepository.save(freshAirVentilator);
        NettyServerHandler.channelGroup.forEach(channel -> {
            if (ipAndPort.equals(channel.remoteAddress().toString())){
                channel.writeAndFlush(JSON.toJSONString(content));
                flag.set(1);
            }else {
                flag.set(0);
            }
        });

        controlExhaustFan(id, isOpen);
        return flag.get();
    }

    private void controlExhaustFan(Integer id, Integer isOpen) {
        ExhaustFan tempExhaustFan = new ExhaustFan();

        if (id == 1) {
            tempExhaustFan = exhaustFanRepository.findById(6).orElseGet(ExhaustFan::new);
            ValidationUtil.isNull(tempExhaustFan.getId(),"ExhaustFan","id",6);
            tempExhaustFan.setIsOpen(isOpen);
            exhaustFanRepository.save(tempExhaustFan);

            tempExhaustFan = exhaustFanRepository.findById(1).orElseGet(ExhaustFan::new);
            ValidationUtil.isNull(tempExhaustFan.getId(),"ExhaustFan","id",1);
            tempExhaustFan.setIsOpen(isOpen);
            exhaustFanRepository.save(tempExhaustFan);
        } else if (id == 2) {
            tempExhaustFan = exhaustFanRepository.findById(8).orElseGet(ExhaustFan::new);
            ValidationUtil.isNull(tempExhaustFan.getId(),"ExhaustFan","id",8);
            tempExhaustFan.setIsOpen(isOpen);
            exhaustFanRepository.save(tempExhaustFan);

            tempExhaustFan = exhaustFanRepository.findById(7).orElseGet(ExhaustFan::new);
            ValidationUtil.isNull(tempExhaustFan.getId(),"ExhaustFan","id",7);
            tempExhaustFan.setIsOpen(isOpen);
            exhaustFanRepository.save(tempExhaustFan);

            tempExhaustFan = exhaustFanRepository.findById(4).orElseGet(ExhaustFan::new);
            ValidationUtil.isNull(tempExhaustFan.getId(),"ExhaustFan","id",4);
            tempExhaustFan.setIsOpen(isOpen);
            exhaustFanRepository.save(tempExhaustFan);

            tempExhaustFan = exhaustFanRepository.findById(3).orElseGet(ExhaustFan::new);
            ValidationUtil.isNull(tempExhaustFan.getId(),"ExhaustFan","id",3);
            tempExhaustFan.setIsOpen(isOpen);
            exhaustFanRepository.save(tempExhaustFan);
        } else if (id == 3) {
            tempExhaustFan = exhaustFanRepository.findById(9).orElseGet(ExhaustFan::new);
            ValidationUtil.isNull(tempExhaustFan.getId(),"ExhaustFan","id",9);
            tempExhaustFan.setIsOpen(isOpen);
            exhaustFanRepository.save(tempExhaustFan);

            tempExhaustFan = exhaustFanRepository.findById(5).orElseGet(ExhaustFan::new);
            ValidationUtil.isNull(tempExhaustFan.getId(),"ExhaustFan","id",5);
            tempExhaustFan.setIsOpen(isOpen);
            exhaustFanRepository.save(tempExhaustFan);

            tempExhaustFan = exhaustFanRepository.findById(2).orElseGet(ExhaustFan::new);
            ValidationUtil.isNull(tempExhaustFan.getId(),"ExhaustFan","id",2);
            tempExhaustFan.setIsOpen(isOpen);
            exhaustFanRepository.save(tempExhaustFan);
        } else if (id == 4) {

        }
    }
}
